package cn.yzking.studentmanagement.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.yzking.studentmanagement.R;
import cn.yzking.studentmanagement.Student;

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
    private final List<Student> mStudentList;
    private OnItemClickListener mItemClickListener;
    private OnItemLongClickListener mItemLongClickListener;

    static class ViewHolder extends RecyclerView.ViewHolder {
        private final ImageView avatar;
        private final TextView name;
        private final TextView id;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.avatar = itemView.findViewById(R.id.avatar);
            this.name = itemView.findViewById(R.id.text_name);
            this.id = itemView.findViewById(R.id.text_id);
        }
    }

    public interface OnItemClickListener {
        void onClick(Student student);
    }

    public interface OnItemLongClickListener {
        void onLongClick(View v, Student student);
    }

    public void setOnItemClickListener(OnItemClickListener l) {
        this.mItemClickListener = l;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener l) {
        this.mItemLongClickListener = l;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_student, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Student student = this.mStudentList.get(position);
        holder.avatar.setImageResource(R.drawable.ic_user);
        holder.name.setText(student.getName());
        holder.id.setText(student.getId());
        holder.itemView.setOnClickListener(v -> {
            if (this.mItemClickListener != null) {
                this.mItemClickListener.onClick(this.mStudentList.get(position));
            }
        });
        holder.itemView.setOnLongClickListener(v -> {
            if (this.mItemLongClickListener != null) {
                this.mItemLongClickListener.onLongClick(v, this.mStudentList.get(position));
                return true;
            }
            return false;
        });
    }

    @Override
    public int getItemCount() {
        return this.mStudentList.size();
    }

    public StudentAdapter() {
        this.mStudentList = new ArrayList<>();
    }

    public void addStudent(Student student) {
        if (!this.mStudentList.isEmpty()) {
            for (Student student2 : this.mStudentList) {
                if (student.getId().equals(student2.getId())) {
                    return;
                }
            }
        }
        this.mStudentList.add(student);
        this.notifyItemInserted(this.getItemCount());
    }
}
